<?php

namespace services\models;

use Yii;

/**
 * This is the model class for table "goods".
 *
 * @property string $goods_id
 * @property integer $source
 * @property integer $status
 * @property integer $category_1
 * @property integer $category_3
 * @property integer $brand_id
 */
class Goods extends \common\models\Goods
{
    public static $STATUS_DESC = ['下线', '下线'];
    public static $_STATUS_ON = 1;
    public static $_STATUS_OFF = 0;


    public static function getDb() {
        return \Yii::$app->db;
    }

    public static function getRedis() {
        return \Yii::$app->redis_goods;
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'goods_id' => 'Goods ID',
            'source' => 'Source',
            'status' => 'Status',
            'category_1' => 'Category 1',
            'category_3' => 'Category 3',
            'brand_id' => 'Brand ID',
        ];
    }
    public function getProducts()
    {
        return $this->hasMany(Product::tableName(), ['goods_id'=>'goods_id']);
    }


    /**
     * 记录领取试用商品的用户列表
     * @param $goods_id
     * @param $bestry_uid
     * @return bool
     */
    public static function tryGoodsAdd($goods_id, $bestry_uid)
    {
        return Yii::$app->redis->zincrby(Yii::$app->params['try_goods_uid'] . $goods_id, 1, $bestry_uid) ==1 ? true : false;
    }

    /**
     * 查找用户是否领取过某件试用商品
     * @param $goods_id
     * @param $bestry_uid
     * @return bool
     */
    public static function tryGoodsConfirm($goods_id, $bestry_uid)
    {
        return Yii::$app->redis->zincrby(Yii::$app->params['try_goods_uid'] . $goods_id, 0, $bestry_uid) > 0 ? true : false;
    }

    /**
     * 把用户从某件试用商品列表中移除
     * @param $goods_id
     * @param $bestry_uid
     */
    public static function tryGoodsRemove($goods_id, $bestry_uid)
    {
        Yii::$app->redis->zadd(Yii::$app->params['try_goods_uid'] . $goods_id, $bestry_uid);
    }

    /**
     * 从缓存取goods_name
     * @param $goods_id
     * @return string
     */
    public static function getNameFromCache($goods_id)
    {
        #todo
        return Yii::$app->redis->hget('todo' . $goods_id, 'goods_name');

    }

    /**
     * 通过商品ID获取商品信息
     * @param int $goods_id 商品ID
     * @return array 商品信息
     */
    public static function findByGoodsId($goods_id) {
        $field_array = ['goods_id', 'source', 'oldid', 'status', 'category_1', 'category_3', 'brand_id', 'goods_status'];
        $row = self::findByGoodsIdFromRedis($goods_id, $field_array);
        if(!empty($row) && $row['goods_id'] > 0) {
            return $row;
        }

        $var = self::find()->where('goods_id=:goods_id', [':goods_id' => $goods_id])->one();
        $array = \yii\helpers\ArrayHelper::toArray($var);
        if(!empty($array)) {
            self::getRedis()->hmset('goods_' . $goods_id, $array);
        }
        return $array;
    }

    /**
     * 通过商品ID，从数据库获取商品信息。
     * @param int $goods_id 商品ID
     * @return array
     */
    public static function findByGoodsIdFromDb($goods_id) {
        $var = self::find()->where('goods_id=:goods_id', [':goods_id' => $goods_id])->one();
        if(empty($var)) {
            return [];
        }

        return \yii\helpers\ArrayHelper::toArray($var);
    }

    /**
     * 通过商品ID从Redis中获取商品信息（可以获取goods_table, goods_detail, goods_ext商品详情除外, goods_price表的信息。）
     * @param int $goods_id 商品ID
     * @param array $field_array 要获取的字段
     * @return array 商品信息
     */
    public static function findByGoodsIdFromRedis($goods_id, $field_array = []) {
        if(empty($field_array)) {
            $field_array = ['status', 'goods_name', 'pic', 'price_type', 'sale_price', 'try_price'];
        }

        return self::getRedis()->hmget('goods_' . $goods_id, $field_array);
    }

    /**
     * 删除Yii框架生成的Redis缓存
     */
    public static function deleteRedisById($goods_id) {
        $product_spec_rows = ProductSpec::findByGoodsId($goods_id);
        foreach($product_spec_rows as $row) {
            self::getRedis()->del('S_P_' . $row['spec_id'] . '_' . $row['spec_value_id'].'_' . $goods_id);
        }

        $product_rows = Product::findByGoodsIdFromDb($goods_id);
        foreach($product_rows as $row) {
            self::getRedis()->del('P_' . $row['product_id']);
            self::getRedis()->del('P_S_' . $row['product_id']);
        }
        unset($product_spec_rows, $product_rows);

        self::getRedis()->del('G_' . $goods_id);
        self::getRedis()->del('SPEC_ALL_' . $goods_id);
    }
}
